package bsts.view.user;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import bsts.application.BSTSApplication;
import bsts.model.UserRole;

public class MainPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private BSTSApplication manager;
	private JPanel pnlMain;
	private JTabbedPane tabMain;
	private CustomerTabPanel tabPnlCustomer;
	private BillTabPanel tabPnlBill;
	private SubscriptionTabPanel tabPnlSubscription;
	private TransactionTabPanel tabPnlTransaction;
	private JPanel pnlButtons;

	private JButton btnLogout;

	private ActionListener actlis_btnLogout;

	public MainPanel(BSTSApplication manager) {
		this.manager = manager;

		pnlMain = new JPanel();
		tabMain = new JTabbedPane();
		tabMain.setPreferredSize(new Dimension(400, 260));
		tabPnlCustomer = new CustomerTabPanel(manager);
		tabPnlBill = new BillTabPanel(manager);
		tabPnlSubscription = new SubscriptionTabPanel(manager);
		tabPnlTransaction = new TransactionTabPanel(manager);
		pnlButtons = new JPanel();

		btnLogout = new JButton("Logout");

		pnlMain.setLayout(new BoxLayout(pnlMain, BoxLayout.Y_AXIS));
		pnlButtons.setLayout(new BorderLayout());

		tabMain.add("Customer", tabPnlCustomer);
		if (manager.getLoginUser().getUserRole().equals(UserRole.Admin)) {
			tabMain.add("Subscription", tabPnlSubscription);
		}
		tabMain.add("Bill", tabPnlBill);
		tabMain.add("Transaction", tabPnlTransaction);

		pnlButtons.add(BorderLayout.EAST, btnLogout);

		pnlMain.add(tabMain);
		pnlMain.add(new JPanel());
		pnlMain.add(pnlButtons);

		this.add(pnlMain);

		initActionListener();
	}

	private void initActionListener() {
		actlis_btnLogout = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.logout();
			}
		};

		btnLogout.addActionListener(actlis_btnLogout);
	}

}
